package com.aacar.op.utils;


import org.apache.commons.lang3.time.DateUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    public static String datePattern = "MM/dd/yyyy";

    public static String defaultPattern2 = "yyyy-MM-dd HH:mm:ss";

    public static String defaultPattern3 = "yyyyMMddHHmmss";

    public static String defaultPattern4 = "yMMddHHmmss";


    /**
     * 几分钟后的时间
     */
    public static Date addDateMinut(Date date, int x) {
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MINUTE, x);// 24小时制
        return cal.getTime();
    }

    //获取当前日期的邮戳
    public static Date getNowDate() {
        return new Date();
    }

    public static final String date2Str(Date aDate) {
        SimpleDateFormat df = null;
        String returnValue = "";

        if (aDate != null) {
            df = new SimpleDateFormat(datePattern);
            returnValue = df.format(aDate);
        }

        return (returnValue);
    }

    public static final String date2Str(String pattern, Date aDate) {
        SimpleDateFormat df = null;
        String returnValue = "";

        if (aDate != null) {
            df = new SimpleDateFormat(pattern);
            returnValue = df.format(aDate);
        }
        return (returnValue);
    }

    public static Date safeStartDate(Date startDate) {
        Date ret = null;
        if (startDate != null) {
            ret = DateUtils.truncate(startDate, Calendar.DAY_OF_MONTH);

        }
        return ret;
    }

    public static Date safeEndDate(Date endDate) {
        Date ret = null;
        if (endDate != null) {
            Date d = DateUtils.addDays(endDate, 1);
            ret = DateUtils.truncate(d, Calendar.DAY_OF_MONTH);

        }
        return ret;
    }
}
